package com.java.forum.c_service;

import com.java.forum.a_entity.Message;
import com.java.forum.b_mapper.MessageMapper;
import com.java.forum.g_exception.Exceptions;
import com.java.forum.g_exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class MessageServiceImpl implements MessageService {
    private MessageMapper messageMapper;

    @Autowired
    public void setMessageMapper(MessageMapper messageMapper) {
        this.messageMapper = messageMapper;
    }

    public Message storeMessage(Message message) {
        Integer res = messageMapper.insertMessage(message);
        if (res != 1) throw new ServiceException(Exceptions.MessageInsertException);
        return message;
    }

    @Override
    public void readMessage(Integer mid) {
        Integer res = messageMapper.updateReadMessage(mid);
        if (res != 1) throw new ServiceException(Exceptions.MessageUpdateException);
    }

    @Override
    public List<Message> getUnreadMessage(Integer uid) {
        return messageMapper.getUnreadMessage(uid);
    }

    @Override
    public List<Message> getHistoryMessage(Date date, Integer uid, Integer uid2) {
        return messageMapper.getHistoryMessage(date, uid, uid2);
    }
}
